from commons.entity.po.user import User

from commons.entity.po.model import db


class Travel(db.Model):
    __tablename__ = 'travel'

    travel_id = db.Column(db.Integer, primary_key=True,
                          autoincrement=True, info='出差编号')
    travel_reason = db.Column(db.String(255), info='出差原因')
    travel_detail = db.Column(db.String(255), info='出差详情')
    apply_date = db.Column(db.Date, info='申请日期')
    start_date = db.Column(db.Date, info='开始日期')
    end_date = db.Column(db.Date, info='结束日期')
    food_cost = db.Column(db.Float, info='餐饮费')
    transport_cost = db.Column(db.Float, info='交通费')
    hotel_cost = db.Column(db.Float, info='住宿费')
    cost_detail = db.Column(db.String(255), info='预算详情')
    advance_payment = db.Column(db.Float, info='预支款')
    user_id = db.Column(db.ForeignKey('user.user_id', ondelete='RESTRICT', onupdate='RESTRICT'), nullable=False,
                        index=True, info='申请人')

    user = db.relationship(
        'User', primaryjoin='Travel.user_id == User.user_id', backref='travels')

    def to_dict(self):
        return {
            'travel_id': self.travel_id,
            'travel_reason': self.travel_reason,
            'travel_detail': self.travel_detail,
            'apply_date': self.apply_date.isoformat() if self.apply_date else None,
            'start_date': self.start_date.isoformat() if self.start_date else None,
            'end_date': self.end_date.isoformat() if self.end_date else None,
            'food_cost': self.food_cost,
            'transport_cost': self.transport_cost,
            'hotel_cost': self.hotel_cost,
            'cost_detail': self.cost_detail,
            'advance_payment': self.advance_payment,
            'user_id': self.user_id
        }

    def to_dict_2(self):
        return {
            'travelId': self.travel_id,
            'travelReason': self.travel_reason,
            'travelDetail': self.travel_detail,
            'applyDate': self.apply_date.isoformat() if self.apply_date else None,
            'startDate': self.start_date.isoformat() if self.start_date else None,
            'endDate': self.end_date.isoformat() if self.end_date else None,
            'foodCost': self.food_cost,
            'transportCost': self.transport_cost,
            'hotelCost': self.hotel_cost,
            'costDetail': self.cost_detail,
            'advancePayment': self.advance_payment,
            'userId': self.user_id
        }
